﻿using HuaweiCloud.Dotnet.Samples.Common;
using HuaweiCloud.Dotnet.Samples.NoSql4Mongo.Components;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace HuaweiCloud.Dotnet.Samples.NoSql4Mongo.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class EquipmentStoreController : ControllerBase
    {
        private readonly IDocRepository<EquipmentStore> _repository;
        public EquipmentStoreController(IDocRepository<EquipmentStore> repository)
        {
            _repository = repository;
        }

        // GET: api/<EquipmentStoreController>
        [HttpGet]
        public async Task<IEnumerable<EquipmentStore>> Get()
        {
            return await _repository.ListAsync();
        }

        // GET api/<EquipmentStoreController>/5
        [HttpGet("{id:length(24)}")]
        public async Task<EquipmentStore?> Get(string id)
        {
            return await _repository.GetByIdAsync(id);
        }

        // POST api/<EquipmentStoreController>
        [HttpPost]
        public async Task Post([FromBody] EquipmentStore value)
        {
            await _repository.AddAsync(value);
        }


        // PUT api/<EquipmentStoreController>/5
        [HttpPut("{id:length(24)}")]
        public async Task Put(string id, [FromBody] EquipmentStore value)
        {
            var item = await _repository.GetByIdAsync(id);
            if(item != null)
            {
                value.Id = item.Id;
                await _repository.UpdateAsync(value);
            }
        }

        // DELETE api/<EquipmentStoreController>/5
        [HttpDelete("{id:length(24)}")]
        public async Task Delete(string id)
        {
            var item = await _repository.GetByIdAsync(id);
            if (item != null)
                await _repository.DeleteAsync(item);
        }
    }
}
